package com.yimils.core.goods.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yimils.core.goods.entity.GoodsHotelTypeEntity;
import com.yimils.core.goods.entity.HotelTypeEntity;
import com.yimils.core.goods.mapper.GoodsHotelTypeMapper;
import com.yimils.core.goods.serivce.GoodsHotelTypeService;
import com.yimils.core.goods.serivce.HotelTypeService;
import com.yimils.core.goods.vo.GoodsHotelTypeRes;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("goodsHotelTypeService")
public class GoodsHotelTypeServiceImpl extends ServiceImpl<GoodsHotelTypeMapper, GoodsHotelTypeEntity> implements GoodsHotelTypeService {
    @Resource(name = "hotelTypeService")
    HotelTypeService hotelTypeService;

    @Override
    public Boolean saveGoodsHotelType(List<Long> hotelTypeIds, Long goodsId) {
        List<GoodsHotelTypeEntity> collect = hotelTypeIds.stream().map(o -> {
            GoodsHotelTypeEntity goodsHotelTypeEntity = new GoodsHotelTypeEntity();
            goodsHotelTypeEntity.setHotelTypeId(o);
            goodsHotelTypeEntity.setGoodsId(goodsId);
            goodsHotelTypeEntity.setState(Boolean.FALSE);
            goodsHotelTypeEntity.setCreateTime(LocalDateTime.now());
            goodsHotelTypeEntity.setUpdateTime(LocalDateTime.now());
            return goodsHotelTypeEntity;
        }).collect(Collectors.toList());

        return saveOrUpdateBatch(collect);
    }

    @Override
    public Boolean deleteGoodsHotelType(List<Long> hotelTypeIds, Long goodsId) {
        QueryWrapper<GoodsHotelTypeEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(GoodsHotelTypeEntity::getGoodsId, goodsId)
                .eq(GoodsHotelTypeEntity::getState, Boolean.FALSE);
        List<GoodsHotelTypeEntity> entities = list(qw);
        entities.forEach(o -> {
            o.setState(Boolean.TRUE);
            o.setUpdateTime(LocalDateTime.now());
        });

        return saveGoodsHotelType(hotelTypeIds, goodsId);
    }

    @Override
    public Map<Long, List<GoodsHotelTypeRes>> mapListGoodsHotelType(List<Long> goodsIds) {
        QueryWrapper<GoodsHotelTypeEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(GoodsHotelTypeEntity::getGoodsId, goodsIds)
                .eq(GoodsHotelTypeEntity::getState, Boolean.FALSE);
        List<GoodsHotelTypeEntity> entities = list(qw);

        List<Long> personIds = entities.stream().map(GoodsHotelTypeEntity::getHotelTypeId).collect(Collectors.toList());
        List<HotelTypeEntity> pictureEntities = hotelTypeService.listHotelType(personIds);
        Map<Long, String> videoMap = pictureEntities.stream().collect(Collectors.toMap(HotelTypeEntity::getId, HotelTypeEntity::getHotelTypeName));
        return entities.stream().map(o -> {
            GoodsHotelTypeRes res = new GoodsHotelTypeRes();
            res.setGoodsId(o.getGoodsId());
            res.setHotelTypeId(o.getHotelTypeId());
            res.setHotelTypeName(videoMap.get(o.getId()));
            return res;
        }).collect(Collectors.groupingBy(GoodsHotelTypeRes::getGoodsId));
    }
}
